package com.example.newland.application82;

import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    private TextView main_tv_jd;
    private TextView main_tv_jf;
    private TextView main_tv_jm;
    private TextView main_tv_wd;
    private TextView main_tv_wf;
    private TextView main_tv_wm;
    private TextView main_tv_jingdu;
    private TextView main_tv_weidu;
    private TextView main_tv_weizhi;
    private String token;
    float jd,jf,jm,wd,wf,wm,a,b,c,d,f,g,h,i;
    int j;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        main_tv_jd = findViewById(R.id.main_tv_jd);
        main_tv_jf = findViewById(R.id.main_tv_jf);
        main_tv_jm = findViewById(R.id.main_tv_jm);
        main_tv_wd = findViewById(R.id.main_tv_wd);
        main_tv_wf = findViewById(R.id.main_tv_wf);
        main_tv_wm = findViewById(R.id.main_tv_wm);
        main_tv_jingdu = findViewById(R.id.main_tv_jingdu);
        main_tv_weidu = findViewById(R.id.main_tv_weidu);
        main_tv_weizhi = findViewById(R.id.main_tv_weizhi);

        SharedPreferences config = getSharedPreferences("config", MODE_PRIVATE);
        token = config.getString("accessToken", "");
        //子线程网络请求
        new Thread(){
            @Override
            public void run() {
                super.run();
                String login_url = "http://api.nlecloud.com/Users/Login";
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("Account","17309339946");
                    jsonObject.put("Password","17309339946");
                    jsonObject.put("IsRememberMe",true);

                    String pamer = jsonObject.toString();
                    String login_json = HttpUrl.post(login_url, pamer, null);
                    Log.i("登录json",login_json);

                    JSONObject jsonObject1 = new JSONObject(login_json);
                    JSONObject resultObj = jsonObject1.getJSONObject("ResultObj");
                    String accessToken = resultObj.getString("AccessToken");
                    Log.i("accessToken",accessToken);

                    SharedPreferences config = getSharedPreferences("config", MODE_PRIVATE);
                    SharedPreferences.Editor edit = config.edit();
                    edit.putString("accessToken",accessToken);
                    edit.commit();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                super.run();
                //请求经度
                while(true){
                    String jd_url = "http://api.nlecloud.com/devices/780686/Datas?deviceId=780686&ApiTags=Longitude1";
                    String jd_json = HttpUrl.get(jd_url, token);
                    //解析json
                    try {
                        JSONObject jsonObject = new JSONObject(jd_json);
                        JSONObject resultObj = jsonObject.getJSONObject("ResultObj");
                        JSONArray dataPoints = resultObj.getJSONArray("DataPoints");
                        JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                        JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                        JSONObject pointDTOJSONObject = pointDTO.getJSONObject(0);
                        String jd_value = pointDTOJSONObject.getString("Value");
                        jd = Float.valueOf(jd_value);
                        Log.i("经度",jd_value);
                        Message message = new Message();
                        message.what = 1;
                        Bundle bundle = new Bundle();
                        bundle.putString("value",jd_value);
                        message.setData(bundle);
                        handler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                super.run();
                //请求经分
                while(true){
                    String jf_url = "http://api.nlecloud.com/devices/780686/Datas?deviceId=780686&ApiTags=Longitude2";
                    String jf_json = HttpUrl.get(jf_url, token);
                    //解析json
                    try {
                        JSONObject jsonObject = new JSONObject(jf_json);
                        JSONObject resultObj = jsonObject.getJSONObject("ResultObj");
                        JSONArray dataPoints = resultObj.getJSONArray("DataPoints");
                        JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                        JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                        JSONObject pointDTOJSONObject = pointDTO.getJSONObject(0);
                        String jf_value = pointDTOJSONObject.getString("Value");
                        Log.i("经分",jf_value);
                        jf = Float.valueOf(jf_value);
                        Message message = new Message();
                        message.what = 2;
                        Bundle bundle = new Bundle();
                        bundle.putString("value",jf_value);
                        message.setData(bundle);
                        handler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                super.run();
                //请求经分
                while(true){
                    String jm_url = "http://api.nlecloud.com/devices/780686/Datas?deviceId=780686&ApiTags=Longitude3";
                    String jm_json = HttpUrl.get(jm_url, token);
                    //解析json
                    try {
                        JSONObject jsonObject = new JSONObject(jm_json);
                        JSONObject resultObj = jsonObject.getJSONObject("ResultObj");
                        JSONArray dataPoints = resultObj.getJSONArray("DataPoints");
                        JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                        JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                        JSONObject pointDTOJSONObject = pointDTO.getJSONObject(0);
                        String jm_value = pointDTOJSONObject.getString("Value");
                        Log.i("经秒",jm_value);
                        jm = Float.valueOf(jm_value);
                        Message message = new Message();
                        message.what = 3;
                        Bundle bundle = new Bundle();
                        bundle.putString("value",jm_value);
                        message.setData(bundle);
                        handler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                super.run();
                //请求经分
                while(true){
                    String wd_url = "http://api.nlecloud.com/devices/780686/Datas?deviceId=780686&ApiTags=Latitude1";
                    String wd_json = HttpUrl.get(wd_url, token);
                    //解析json
                    try {
                        JSONObject jsonObject = new JSONObject(wd_json);
                        JSONObject resultObj = jsonObject.getJSONObject("ResultObj");
                        JSONArray dataPoints = resultObj.getJSONArray("DataPoints");
                        JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                        JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                        JSONObject pointDTOJSONObject = pointDTO.getJSONObject(0);
                        String wd_value = pointDTOJSONObject.getString("Value");
                        Log.i("纬度",wd_value);
                        wd = Float.valueOf(wd_value);
                        Message message = new Message();
                        message.what = 4;
                        Bundle bundle = new Bundle();
                        bundle.putString("value",wd_value);
                        message.setData(bundle);
                        handler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                super.run();
                //请求经分
                while(true){
                    String wf_url = "http://api.nlecloud.com/devices/780686/Datas?deviceId=780686&ApiTags=Latitude2";
                    String wf_json = HttpUrl.get(wf_url, token);
                    //解析json
                    try {
                        JSONObject jsonObject = new JSONObject(wf_json);
                        JSONObject resultObj = jsonObject.getJSONObject("ResultObj");
                        JSONArray dataPoints = resultObj.getJSONArray("DataPoints");
                        JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                        JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                        JSONObject pointDTOJSONObject = pointDTO.getJSONObject(0);
                        String wf_value = pointDTOJSONObject.getString("Value");
                        Log.i("纬分",wf_value);
                        wf = Float.valueOf(wf_value);
                        Message message = new Message();
                        message.what = 5;
                        Bundle bundle = new Bundle();
                        bundle.putString("value",wf_value);
                        message.setData(bundle);
                        handler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                super.run();
                //请求经分
                while(true){
                    String wm_url = "http://api.nlecloud.com/devices/780686/Datas?deviceId=780686&ApiTags=Latitude3";
                    String wm_json = HttpUrl.get(wm_url, token);
                    //解析json
                    try {
                        JSONObject jsonObject = new JSONObject(wm_json);
                        JSONObject resultObj = jsonObject.getJSONObject("ResultObj");
                        JSONArray dataPoints = resultObj.getJSONArray("DataPoints");
                        JSONObject jsonObject1 = dataPoints.getJSONObject(0);
                        JSONArray pointDTO = jsonObject1.getJSONArray("PointDTO");
                        JSONObject pointDTOJSONObject = pointDTO.getJSONObject(0);
                        String wm_value = pointDTOJSONObject.getString("Value");
                        Log.i("纬秒",wm_value);
                        wm = Float.valueOf(wm_value);
                        Message message = new Message();
                        message.what = 6;
                        Bundle bundle = new Bundle();
                        bundle.putString("value",wm_value);
                        message.setData(bundle);
                        handler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                super.run();
                //计算经度
                while(true){
                    a = jm / 60;
                    b = jf + a;
                    c = b / 60;
                    d = jd + c;
                    Log.e("经度", String.valueOf(d));

                    i = wm / 60;
                    f = wf + i;
                    g = f / 60;
                    h = wd + g;

                    Log.e("纬度", String.valueOf(h));

                    if((97.527278 < d && d < 106.196958) && (21.142312 < h && h < 29.225286)){

                        String alarm = "http://api.nlecloud.com/Cmds?deviceId=778003&apiTag=m_rotating_lamp";
                        HttpUrl.post(alarm,"0",token);
                        j = 1;
                    }else{
                        String alarm = "http://api.nlecloud.com/Cmds?deviceId=778003&apiTag=m_rotating_lamp";
                        HttpUrl.post(alarm,"1",token);
                        j = 2;
                    }

                    Message message = new Message();
                    message.what = 7;

                    Bundle bundle = new Bundle();
                    bundle.putString("jd", String.valueOf(d));
                    bundle.putString("wd", String.valueOf(h));
                    bundle.putString("status", String.valueOf(j));
                    message.setData(bundle);

                    handler.sendMessage(message);
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what){
                case 1:
                    Bundle data1 = msg.getData();
                    String value1 = data1.getString("value");
                    main_tv_jd.setText(value1);
                    break;
                case 2:
                    Bundle data2 = msg.getData();
                    String value2 = data2.getString("value");
                    main_tv_jf.setText(value2);
                    break;
                case 3:
                    Bundle data3 = msg.getData();
                    String value3 = data3.getString("value");
                    main_tv_jm.setText(value3);
                    break;
                case 4:
                    Bundle data4 = msg.getData();
                    String value4 = data4.getString("value");
                    main_tv_wd.setText(value4);
                    break;
                case 5:
                    Bundle data5 = msg.getData();
                    String value5 = data5.getString("value");
                    main_tv_wf.setText(value5);
                    break;
                case 6:
                    Bundle data6 = msg.getData();
                    String value6 = data6.getString("value");
                    main_tv_wm.setText(value6);
                    break;
                case 7:
                    Bundle data7 = msg.getData();
                    String jd_value = data7.getString("jd");
                    String wd_value = data7.getString("wd");
                    String status_value = data7.getString("status");
                    main_tv_jingdu.setText(jd_value);
                    main_tv_weidu.setText(wd_value);
                    Integer integer = Integer.valueOf(status_value);
                    if(integer == 1){
                        main_tv_weizhi.setText("云南省");
                    }else if(integer == 2){
                        main_tv_weizhi.setText("区域外");
                    }
                    break;
            }
        }
    };
}
